package app.ui.panels {
	import app.auth.ClientLogin;

	import com.bit101.components.CheckBox;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;

	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.text.TextField;

	/**
	 * @author Rendall
	 */
	public class LoginPanel extends AbstractBrowserPanel {
		private static const ENTER_CODE : uint = 13;

		private var clientLogin : ClientLogin;
		private var statusText : TextArea;
		private var userEmailInput : InputText;
		private var passwordInput : InputText;
		private var sendButton : PushButton;
		private var hidePassword : CheckBox;

		public function LoginPanel(parent : DisplayObjectContainer = null, xpos : Number = 0, ypos : Number = 0) {
			super(parent, xpos, ypos);
			
			setUI();
				
			clientLogin = new ClientLogin();
			clientLogin.statusSignal.add(onStatusChange);

			var isStandAlone : Boolean = this.parent != null;
			if (isStandAlone) standAloneInit();
		}

		private function setUI() : void {
			statusText = new TextArea(this);
			statusText.setSize(550, 20);
			statusText.html = true;
			
			var userEmailLabel : Label = new Label(this, 5, 35, "Email: ");
			var emailX : Number = userEmailLabel.x + userEmailLabel.width + 5;
			userEmailInput = new InputText(this, emailX, 35);

			var userPasswordLabel : Label = new Label(this, 5, 65, "Password: ");
			var pwdX : Number = userPasswordLabel.x + userPasswordLabel.width + 5;
			passwordInput = new InputText(this, pwdX, 65);
			passwordInput.password = true;
			
			hidePassword = new CheckBox(this, pwdX + passwordInput.width + 5, 68, "Hide password", onShowPassword);
			hidePassword.selected = true;
			
			sendButton = new PushButton(this, 5, 95, "SUBMIT", onPressInput);
			sendButton.tabEnabled = true;
		}

		private function onShowPassword(event : Event) : void {
			passwordInput.password = hidePassword.selected;
		}

		private function onKeyInput(changeEvent : Event) : void {
			var inputString : String = TextField(changeEvent.target).text;
			var lastChar : uint = inputString.charCodeAt(inputString.length - 1);
			if (lastChar == ENTER_CODE) onInput();
		}

		private function onPressInput(input : *) : void {
			trace("\n LoginPanel.onPressInput( input:" + input + ")");
			onInput();
		}

		private function onInput() : void {
			var email : String = userEmailInput.text;
			validateEmail(email);
			
			var password : String = passwordInput.text;
			validatePassword(password);
			
			clientLogin.authenticate(email, password);
		}

		private function validatePassword(password : String) : void {
		}

		private function validateEmail(email : String) : void {
		}

		private function onStatusChange(status : String, message : String) : void {

			switch (status) {
				case ClientLogin.STATUS_READY: 
					message = "Please enter your google email and password to continue.";
					if (stage) stage.focus = userEmailInput.textField;
					break;                    
				case ClientLogin.STATUS_REQUEST_SENT:
					message = "Please wait."; break;
				case ClientLogin.STATUS_ERROR:
					message = "An error has occurred. Please login at <u><a href='http://google.com/calendar' target='_blank'>http://google.com/calendar</a></u> and then resubmit.";
					break;
			}
			
			
			var statusMessage : String = status;
			if (message != null) statusMessage += " : " + message;
			
			var firstText : Boolean = LoginPanel.isEmptyString(statusText.text);
			if (!firstText) statusText.text = statusMessage + "\n" + statusText.text;
			else statusText.text = statusMessage;
		
			trace("\n LoginPanel.onStatusChange( status:" + status + ")");
		}

		// For debugging purposes, if this is testing the LoginPanel as a lone swf.
		private function standAloneInit() : void {
			trace("\n LoginPanel.standAloneInit( )");
			this.width = 550;
			this.height = 400;
		}

		private static function isEmptyString(str : String) : Boolean {
			return (str == null || str == "" || str.length == 0);	
		}
	}
}
